package cn.iocoder.yudao.module.pplan.service.tableware;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.iocoder.yudao.module.pplan.controller.admin.tableware.vo.*;
import cn.iocoder.yudao.module.pplan.dal.dataobject.tableware.TablewareDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.pplan.dal.mysql.tableware.TablewareMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.diffList;
import static cn.iocoder.yudao.module.pplan.enums.ErrorCodeConstants.*;

/**
 * 餐具管理 Service 实现类
 *
 * @author yisl
 */
@Service
@Validated
public class TablewareServiceImpl implements TablewareService {

    @Resource
    private TablewareMapper tablewareMapper;

    @Override
    public Long createTableware(TablewareSaveReqVO createReqVO) {
        // 校验存在
        if (createReqVO.getName() != null) {
            validateTablewareExists(createReqVO.getName());
        }
        // 插入
        TablewareDO tableware = BeanUtils.toBean(createReqVO, TablewareDO.class);
        tablewareMapper.insert(tableware);
        // 返回
        return tableware.getId();
    }

    @Override
    public void updateTableware(TablewareSaveReqVO updateReqVO) {
        // 校验存在
        validateTablewareExists(updateReqVO.getId());
        // 校验存在
        if (updateReqVO.getName() != null) {
            validateTablewareExists(updateReqVO.getName());
        }
        // 更新
        TablewareDO updateObj = BeanUtils.toBean(updateReqVO, TablewareDO.class);
        tablewareMapper.updateById(updateObj);
    }

    @Override
    public void deleteTableware(Long id) {
        // 校验存在
        validateTablewareExists(id);
        // 删除
        tablewareMapper.deleteById(id, false);
    }

    @Override
        public void deleteTablewareListByIds(List<Long> ids) {
        // 校验存在
        validateTablewareExists(ids);
        // 删除
        tablewareMapper.deleteByIds(ids, false);
        }

    private void validateTablewareExists(List<Long> ids) {
        List<TablewareDO> list = tablewareMapper.selectByIds(ids);
        if (CollUtil.isEmpty(list) || list.size() != ids.size()) {
            throw exception(TABLEWARE_NOT_EXISTS);
        }
    }

    private void validateTablewareExists(Long id) {
        if (tablewareMapper.selectById(id) == null) {
            throw exception(TABLEWARE_NOT_EXISTS);
        }
    }

    private void validateTablewareExists(String name) {
        if (tablewareMapper.selectCount("name",name) != 0) {
            throw exception(TABLEWARE_NAME_EXISTS);
        }
    }

    @Override
    public TablewareDO getTableware(Long id) {
        return tablewareMapper.selectById(id);
    }

    @Override
    public PageResult<TablewareDO> getTablewarePage(TablewarePageReqVO pageReqVO) {
        return tablewareMapper.selectPage(pageReqVO);
    }

}